#!/bin/bash
#更换阿里源
#sed -i 's/tencentyun/aliyun/g' /etc/apt/sources.list

#更新软件列表
#apt-get update

#安装svn
#apt-get install -y subversion

#新建一个用户
#/usr/sbin/useradd -m -u 1536 judge

#切换到用户目录
# shellcheck disable=SC2164
#cd /home/judge/

#using tgz src files
#下载并解压hustoj.tar.gz为src
#wget -O hustoj.tar.gz http://dl.hustoj.com/hustoj.tar.gz
#tar xzf hustoj.tar.gz

#svn更新src
#svn up src
#svn co https://github.com/zhblue/hustoj/trunk/trunk/  src

#下载各种包
#for pkg in "net-tools make flex g++ clang libmysqlclient-dev libmysql++-dev php-fpm nginx mysql-server php-mysql  php-common php-gd php-zip fp-compiler openjdk-11-jdk mono-devel php-mbstring php-xml php-curl php-intl php-xmlrpc php-soap"
#do
#	while ! apt-get install -y $pkg
#	do
#		echo "Network fail, retry... you might want to change another apt source for install"
#	done
#done

#数据库用户名
USER='root'
#数据库密码
PASSWORD='123456'
#cpu核心数
CPU=`grep "cpu cores" /proc/cpuinfo |head -1|awk '{print $4}'`

#用户下新建多个文件夹
mkdir etc data log backup

#建立java权限设置文件
cp src/install/java0.policy  /home/judge/etc
#建立核心配置文件
cp src/install/judge.conf  /home/judge/etc
#脚本添加执行权限
chmod +x src/install/ans2out

#如果未开启/dev/shm来运行答案,创建四个run目录，并更改其所有者为judge
if grep "OJ_SHM_RUN=0" etc/judge.conf ; then
	mkdir run0 run1 run2 run3
	chown judge run0 run1 run2 run3
fi

#替换配置文件中的信息
sed -i "s/OJ_USER_NAME=root/OJ_USER_NAME=$USER/g" etc/judge.conf
sed -i "s/OJ_PASSWORD=root/OJ_PASSWORD=$PASSWORD/g" etc/judge.conf
sed -i "s/OJ_DB_NAME=goj/g" etc/judge.conf
sed -i "s/OJ_REDISAUTH=1234567/g" etc/judge.conf
sed -i "s/OJ_REDISQNAME=goj/g" etc/judge.conf
sed -i "s/OJ_COMPILE_CHROOT=1/OJ_COMPILE_CHROOT=0/g" etc/judge.conf
sed -i "s/OJ_RUNNING=1/OJ_RUNNING=$CPU/g" etc/judge.conf

#更改权限只支持root
chmod 700 backup
chmod 700 etc/judge.conf

#配置web的数据库连接用户名与密码
#sed -i "s/DB_USER[[:space:]]*=[[:space:]]*\"root\"/DB_USER=\"$USER\"/g" src/web/include/db_info.inc.php
#sed -i "s/DB_PASS[[:space:]]*=[[:space:]]*\"root\"/DB_PASS=\"$PASSWORD\"/g" src/web/include/db_info.inc.php

#更改权限只支持root
#chmod 700 src/web/include/db_info.inc.php

#修改所属者为系统用户www-data
#chown -R www-data src/web/

#修改svn的用户和权限
#chown -R root:root src/web/.svn
#chmod 750 -R src/web/.svn

#修改用户
#chown www-data src/web/upload data

#如果nginx上传文件限制不存在则添加限制
#if grep "client_max_body_size" /etc/nginx/nginx.conf ; then 
#	echo "client_max_body_size already added" ;
#else
#	sed -i "s:include /etc/nginx/mime.types;:client_max_body_size    80m;\n\tinclude /etc/nginx/mime.types;:g" /etc/nginx/nginx.conf
#fi

#mysql初始化数据库，插入两个用户
#mysql -h localhost -u$USER -p$PASSWORD < src/install/db.sql
#echo "insert into jol.privilege values('admin','administrator','N');"|mysql -h localhost -u$USER -p$PASSWORD 

#nginx配置
#if grep "added by hustoj" /etc/nginx/sites-enabled/default ; then
#	echo "default site modified!"
#else
#	echo "modify the default site"
#	sed -i "s#root /var/www/html;#root /home/judge/src/web;#g" /etc/nginx/sites-enabled/default
#	sed -i "s:index index.html:index index.php:g" /etc/nginx/sites-enabled/default
#	sed -i "s:#location ~ \\\.php\\$:location ~ \\\.php\\$:g" /etc/nginx/sites-enabled/default
#	sed -i "s:#\tinclude snippets:\tinclude snippets:g" /etc/nginx/sites-enabled/default
#	sed -i "s|#\tfastcgi_pass unix|\tfastcgi_pass unix|g" /etc/nginx/sites-enabled/default
#	sed -i "s:}#added by hustoj::g" /etc/nginx/sites-enabled/default
#	sed -i "s:php7.0:php7.2:g" /etc/nginx/sites-enabled/default
#	sed -i "s|# deny access to .htaccess files|}#added by hustoj\n\n\n\t# deny access to .htaccess files|g" /etc/nginx/sites-enabled/default
#fi

#重启nginx，网站信息配置
#/etc/init.d/nginx restart
#sed -i "s/post_max_size = 8M/post_max_size = 80M/g" /etc/php/7.2/fpm/php.ini
#sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 80M/g" /etc/php/7.2/fpm/php.ini
#sed -i 's/;request_terminate_timeout = 0/request_terminate_timeout = 128/g' `find /etc/php -name www.conf`
#sed -i 's/pm.max_children = 5/pm.max_children = 200/g' `find /etc/php -name www.conf`

#计算cpu数据调整性能
COMPENSATION=`grep 'mips' /proc/cpuinfo|head -1|awk -F: '{printf("%.2f",$2/5000)}'`
sed -i "s/OJ_CPU_COMPENSATION=1.0/OJ_CPU_COMPENSATION=$COMPENSATION/g" etc/judge.conf

#重启php
#/etc/init.d/php7.2-fpm restart
#service php7.2-fpm restart

#评测机安装
cd src/core
chmod +x ./make.sh
./make.sh

#添加开机自启
if grep "/usr/bin/judged" /etc/rc.local ; then
	echo "auto start judged added!"
else
	sed -i "s/exit 0//g" /etc/rc.local
	echo "/usr/bin/judged" >> /etc/rc.local
	echo "exit 0" >> /etc/rc.local
fi

#自动备份数据库
if grep "bak.sh" /var/spool/cron/crontabs/root ; then
	echo "auto backup added!"
else
	crontab -l > conf && echo "1 0 * * * /home/judge/src/install/bak.sh" >> conf && crontab conf && rm -f conf
fi
#建立软连接
ln -s /usr/bin/mcs /usr/bin/gmcs

#启动评测服务
/usr/bin/judged
cp /home/judge/src/install/goj /etc/init.d/goj

#goj注册为守护进程
update-rc.d goj defaults
systemctl enable goj
#systemctl enable nginx
#systemctl enable mysql
#systemctl enable php7.2-fpm

#cls
#reset

echo "username:$USER"
echo "password:$PASSWORD"
